<script setup lang="ts">
defineOptions({
  name: "Loginlog",
  inheritAttrs: false,
});
import {
  getLoginLogPage,
  removeLoginLogs,
  removeAllLoginLogs,
  exportLoginLog,
} from "@/api/loginlog";
import { LoginLogPageVO, LoginLogQuery } from "@/api/loginlog/types";
import BackTop from "@/components/BackTop/index.vue";
import { downloadBlob } from "@/utils/file";

const tabloading = ref(false);
const queryFormRef = ref(ElForm); // 查询表单
/** 查询参数 */
const queryParams = reactive<LoginLogQuery>({
  pageNum: 1,
  pageSize: 10,
});
/** 日志数据 */
const logs = ref<LoginLogPageVO[]>();
/** 日志总量 */
const logsAmount = ref(0);
/** 被选中的日志 */
const ids = ref<string[]>([]);

/** 删除选中项 */
function remove(isAll: boolean) {
  if (isAll) {
    ElMessageBox.confirm("确认要清空当前全部日志吗？该操作不可逆！", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      removeAllLoginLogs().then(() => {
        ElMessage.success("删除成功");
        resetQuery();
      });
    });
  } else {
    const logIds = ids.value.join(",");
    if (!logIds) {
      ElMessage.warning("请勾选删除项");
      return;
    }

    ElMessageBox.confirm("确认删除日志?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(function () {
      removeLoginLogs(logIds).then(() => {
        ElMessage.success("删除成功");
        resetQuery();
      });
    });
  }
}

/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/**
 * 刷新表单。
 */
function handleQuery() {
  tabloading.value = true;
  getLoginLogPage(queryParams)
    .then(({ data }) => {
      logs.value = data.list;
      logsAmount.value = data.total;
    })
    .finally(() => {
      tabloading.value = false;
    });
}

/**
 * 多选框勾选变化事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

/**
 * 导出登录日志记录
 */
function handleUserExport() {
  exportLoginLog()
    .then((response: any) => {
      downloadBlob(response);
    })
    .finally(() => {});
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div>
    <div class="search-container f-card">
      <el-form ref="queryFormRef" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="用户名/昵称/手机号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button v-throttle="handleQuery" type="primary"
            ><i-ep-search />搜索</el-button
          >
          <el-button v-throttle="resetQuery">
            <i-ep-refresh />
            重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-card class="f-card">
      <el-row style="margin-bottom: 15px">
        <el-button
          v-throttle="() => remove(false)"
          type="danger"
          plain
          :disabled="ids.length === 0"
        >
          <el-icon>
            <Delete />
          </el-icon>
          删除
        </el-button>
        <el-button v-throttle="() => remove(true)" type="danger" plain>
          <el-icon>
            <Delete />
          </el-icon>
          清空
        </el-button>
        <el-button v-throttle="handleUserExport" type="warning" plain>
          <el-icon>
            <Download />
          </el-icon>
          导出
        </el-button>
      </el-row>
      <el-table
        v-loading="tabloading"
        :data="logs"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" />
        <el-table-column prop="id" label="序号" width="60" />
        <el-table-column prop="username" label="用户名称" sortable />
        <el-table-column prop="ip" label="登陆地址" />
        <el-table-column prop="loginAddress" label="登陆地点" />
        <el-table-column prop="browser" label="浏览器" />
        <el-table-column prop="operatingSystem" label="操作系统" />
        <el-table-column prop="loginDate" label="登陆日期" sortable />
      </el-table>
      <pagination
        v-if="logsAmount > 0"
        v-model:total="logsAmount"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
      <back-top></back-top>
    </el-card>
  </div>
</template>

<style scoped>
.f-card {
  margin: 1em;
}
</style>
